#pragma once
#include<vector>
#include<functional>
#include<assert.h>
namespace wcz
{
    using std::swap;
    template <class T>
    struct less
    {
        bool operator()(const T& x, const T& y)const
        {
            return x < y;
        }
    };
    template <class T>
    struct greater
    {
        bool operator()(const T& x, const T& y)const
        {
            return x > y;
        }
    };
    template <class T, class Container = std::vector<T>, class Compare = less<T> >
    class priority_queue
    {
    public:
        priority_queue() = default;

        template <class InputIterator>
        priority_queue(InputIterator first, InputIterator last)
        {
            while (first != last)
                push(*first++);
        }

        bool empty() const { return _c.empty(); }
        size_t size() const { return _c.size(); }
        const T& top() const { return _c[0]; }

        void AdjustUp(int child)
        {
            int parent = (child - 1) / 2;
            while (child > 0)
            {
                if (_comp(_c[parent], _c[child]))
                {
                    swap(_c[parent], _c[child]);
                    child = parent;
                    parent = (child - 1) / 2;
                }
                else
                {
                    break;
                }
            }
        }

        void AdjustDown(int parent)
        {
            int child = parent * 2 + 1;
            while (child < size())
            {
                if (child + 1 < size() && _comp(_c[child], _c[child + 1]))
                    ++child;

                if (_comp(_c[parent], _c[child]))
                {
                    swap(_c[parent], _c[child]);
                    parent = child;
                    child = parent * 2 + 1;
                }
                else
                {
                    break;
                }
            }
        }
        void push(const T& x)
        {
            _c.push_back(x);
            AdjustUp(size() - 1);
        }

        void pop()
        {
            assert(!_c.empty());
            swap(_c[0], _c[size() - 1]);
            _c.pop_back();
            AdjustDown(0);
        }

    private:
        Container _c;
        Compare _comp;
    };

};